package com.item.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.User;
import com.item.mall.service.CartService;
import com.item.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private CartService cartService;

    @PostMapping("/register")
    public String register(User user, Model model){
        boolean result = false;
        try {
            result = userService.save(user);
        } catch (Exception e) {
            model.addAttribute("error",user.getLoginName()+"已经存在，请重新输入！");
            e.printStackTrace();
            return "register";
        }
        if(result)  return "login";
        return "register";
    }

    /**
     * 登录
     * @param loginName
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(String loginName, String password , HttpSession session,Model model){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_name",loginName);
        wrapper.eq("password",password);
        User user =userService.getOne(wrapper);
        if(user == null){
            model.addAttribute("error","用户名或密码错误！");
            return "login";
        }else {
            session.setAttribute("user",user);
            return "redirect:/productCategory/list";
        }

    }

    /**
     * 注销
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout( HttpSession session){
        session.invalidate();
        return "login";
    }

    @GetMapping("/info")
    public ModelAndView getInfo(HttpSession session){
        ModelAndView modelAndView =new ModelAndView();
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("nowUser",userService.getById(user.getId()));
        modelAndView.setViewName("userInfo");
        return modelAndView;
    }

    @PostMapping("/upload")
    public ModelAndView upload(@RequestParam(value = "touxiang") MultipartFile file, HttpSession session) throws IOException, ServletException {
        ModelAndView modelAndView =new ModelAndView("redirect:/user/info");
        User user =(User) session.getAttribute("user");
        userService.updateFileName(file,user.getId());
        return modelAndView;
    }

    @GetMapping("/update/{id}/{userName}/{gender}/{mobile}/{email}")
    public  ModelAndView update(User user){
        ModelAndView modelAndView = new ModelAndView("userInfo");
        userService.updateById(user);
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("nowUser",userService.getById(user.getId()));
        return modelAndView;
    }

}

